import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sp_util/sp_util.dart';
import './adapt.dart';
import './transition_type.dart';
import 'package:get/get.dart';
import '../../../router.dart';
import '../../../helper.dart';

import 'package:common_utils/common_utils.dart';
import 'package:intl/intl.dart';

class ShoppingVoucherDialog extends Dialog {
  final int voucherNum;
  final List voucherList;
  final bool isForce;

  ShoppingVoucherDialog({this.voucherList, this.voucherNum, this.isForce});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
          child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            child: GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: Container(
                child: Image.asset(
                  'assets/images/icon_close.png',
                  fit: BoxFit.contain,
                  width: Adapt.px(65),
                  height: Adapt.px(65),
                ),
              ),
            ),
          ),
          Container(
              margin: EdgeInsets.fromLTRB(0, Adapt.px(20), 0, Adapt.px(60)),
              width: Adapt.px(521),
              height: Adapt.px(740),
              child: Stack(children: [
                Positioned(
                  top: 0,
                  left: 0,
                  child: Image.asset(
                    'assets/images/coupon_bg.png',
                    fit: BoxFit.fill,
                    width: Adapt.px(521),
                    height: Adapt.px(740),
                  ),
                ),
                Positioned(
                    top: Adapt.px(32),
                    left: Adapt.px(62),
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Text(
                            '福利券',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: Adapt.px(50),
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          Text(
                            '恭喜您，获得${voucherNum}张优惠券',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: Adapt.px(28),
                            ),
                          ),
                          Text(
                            '已放入我的-优惠券列表',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: Adapt.px(28),
                            ),
                          ),
                          _voucherListWidget(context),
                        ])),
              ])),
          Container(
            width: Adapt.px(433),
            height: Adapt.px(66),
            alignment: Alignment.topCenter,
            decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage('assets/images/icon_button.png'),
                  fit: BoxFit.fill),
            ),
            child: FlatButton(
              onPressed: () {
                toast('已放入我的-优惠券列表');
                Navigator.pop(context);
              },
              child: Text(
                '我知道了',
                style: TextStyle(
                  fontSize: Adapt.px(32),
                  color: Colors.white,
                ),
              ),
              color: Colors.transparent,
            ),
          )
        ],
      )),
    );
  }

  Widget _voucherListWidget(BuildContext context) {
    return Container(
      width: Adapt.px(400),
      height: Adapt.px(377),
      margin: EdgeInsets.fromLTRB(0, Adapt.px(94), 0, 0),
      // child: Scrollbar(
      //   thickness: 2,
      child: ListView.builder(
        itemCount: voucherList.length,
        itemBuilder: (context, index) {
          return Container(
            width: Adapt.px(400),
            height: Adapt.px(115),
            margin: EdgeInsets.fromLTRB(0, 0, 0, Adapt.px(17)),
            padding: EdgeInsets.fromLTRB(
                Adapt.px(9), Adapt.px(16), Adapt.px(9), Adapt.px(16)),
            decoration: BoxDecoration(
              image: DecorationImage(
                  image: ExactAssetImage('assets/images/coupon_item_bg.png'),
                  fit: BoxFit.fill),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                _showMoneyInfo(voucherList[index]),
                Container(
                    width: Adapt.px(228),
                    // decoration: new BoxDecoration(
                    //   color: Colors.yellow,
                    // ),
                    constraints: BoxConstraints(maxWidth: Adapt.px(228)),
                    padding: EdgeInsets.fromLTRB(Adapt.px(18), 0, 0, 0),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          margin: EdgeInsets.fromLTRB(0, 0, 0, Adapt.px(5)),
                          child: Text(
                            voucherList[index]["title"],
                            textAlign: TextAlign.left,
                            style: TextStyle(
                                fontSize: Adapt.px(26), color: Colors.white),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        Text.rich(
                          (voucherList[index]["use_begin_date"] == null &&
                                  voucherList[index]["use_begin_end"] == null)
                              ? (TextSpan(children: [
                                  voucherList[index]["fixed_begin_term"] == 0
                                      ? (TextSpan(
                                          text: '剩余',
                                          style: TextStyle(
                                            color: Colors.white,
                                            fontSize: Adapt.px(16),
                                          ),
                                        ))
                                      : (TextSpan(
                                          text: '领券起',
                                          style: TextStyle(
                                            color: Colors.white,
                                            fontSize: Adapt.px(16),
                                          ),
                                        )),
                                  TextSpan(
                                    text:
                                        '${voucherList[index]["fixed_term"]}天有效',
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: Adapt.px(16),
                                    ),
                                  ),
                                ]))
                              : (TextSpan(
                                  text:
                                      '${voucherList[index]["use_begin_date"]}-${voucherList[index]["use_begin_end"]}',
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: Adapt.px(16),
                                  ),
                                )),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                        Text(
                          '券来源：${voucherList[index]['award_name']}',
                          style: TextStyle(
                              color: Colors.white, fontSize: Adapt.px(16)),
                        )
                      ],
                    )),
                Container(
                  constraints: BoxConstraints(maxWidth: Adapt.px(50)),
                  width: Adapt.px(50),
                  height: Adapt.px(85),
                  padding:
                      EdgeInsets.fromLTRB(Adapt.px(15), 0, Adapt.px(15), 0),
                  child: GestureDetector(
                    onTap: () {
                      var item = voucherList[index];
                      if (item['sdate'] != '' &&
                          item['edate'] != '' &&
                          item['sdate'] != null &&
                          item['edate'] != null) {
                        var startDate = DateTime.tryParse(item['sdate']);
                        var endDate = DateTime.tryParse(item['edate']);
                        var disable = (DateTime.now().isBefore(startDate)) ||
                            (DateTime.now().isAfter(endDate));
                        if (disable) {
                          toast('未到优惠券使用时间');
                          return false;
                        }
                      }
                      Navigator.pop(context);
                      Get.toNamed(RouteConfig.couponGoodsList, arguments: {
                        'coupon_id': voucherList[index]['coupon_id'],
                      });
                    },
                    child: Text(
                      '去使用',
                      style: TextStyle(
                          fontSize: Adapt.px(20),
                          color: Colors.white,
                          fontWeight: FontWeight.bold),
                      softWrap: true,
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
      //),
    );
  }

  Widget _showMoneyInfo(voucher) {
    if (voucher["coupontype"] == 2) {
      return Container(
        width: Adapt.px(104),
        constraints: BoxConstraints(maxWidth: Adapt.px(104)),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            RichText(
              textAlign: TextAlign.center,
              text: TextSpan(children: [
                TextSpan(
                  text: '免邮',
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: Adapt.px(40),
                      fontWeight: FontWeight.bold),
                )
              ]),
            ),
            voucher["least_cost"] > 0
                ? (Text(
                    '满${voucher["least_cost"]}元可用',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: Adapt.px(16),
                    ),
                  ))
                : (Text(
                    '无门槛',
                    textAlign: TextAlign.left,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: Adapt.px(16),
                    ),
                  )),
          ],
        ),
      );
    } else if (voucher["coupontype"] == 0) {
      return Container(
        width: Adapt.px(104),
        constraints: BoxConstraints(maxWidth: Adapt.px(104)),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            RichText(
              textAlign: TextAlign.center,
              text: TextSpan(children: [
                TextSpan(
                  text: (voucher["face_value"] / 10).toStringAsFixed(2),
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: Adapt.px(36),
                      fontWeight: FontWeight.bold),
                ),
                TextSpan(
                  text: '折',
                  style: TextStyle(
                      fontSize: Adapt.px(14),
                      color: Colors.white,
                      fontWeight: FontWeight.bold),
                ),
              ]),
            ),
            voucher["least_cost"] > 0
                ? (Text(
                    '满${voucher["least_cost"]}元可用',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: Adapt.px(16),
                    ),
                  ))
                : (Text(
                    '无门槛',
                    textAlign: TextAlign.left,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: Adapt.px(16),
                    ),
                  )),
          ],
        ),
      );
    } else {
      return Container(
        width: Adapt.px(104),
        constraints: BoxConstraints(maxWidth: Adapt.px(104)),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            RichText(
              textAlign: TextAlign.center,
              overflow: TextOverflow.ellipsis,
              text: TextSpan(
                text: '￥ ',
                style: TextStyle(
                  fontSize: Adapt.px(14),
                  color: Colors.white,
                ),
                children: [
                  TextSpan(
                    text: voucher["face_value"]
                            ?.toString()
                            .replaceAll('', '\u200B') ??
                        '',
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: voucher["face_value"]?.toString().length > 5
                            ? Adapt.px(30)
                            : Adapt.px(36),
                        fontWeight: FontWeight.bold),
                  )
                ],
              ),
            ),
            voucher["least_cost"] > 0
                ? (Text(
                    '满${voucher["least_cost"]}元可用',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: Adapt.px(16),
                    ),
                  ))
                : (Text(
                    '无门槛',
                    textAlign: TextAlign.left,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: Adapt.px(16),
                    ),
                  )),
          ],
        ),
      );
    }
  }

  static showShoppingVoucherDialog(
      BuildContext context, List mVoucherList, int mVoucherNum, bool mIsForce) {
    return showAnimationDialog(
        barrierDismissible: false,
        transitionType: TransitionType.scale,
        context: context,
        builder: (BuildContext context) {
          return WillPopScope(
            child: ShoppingVoucherDialog(
                voucherList: mVoucherList,
                voucherNum: mVoucherNum,
                isForce: mIsForce),
            onWillPop: _onWillPop,
          );
        });
  }

  static Future<bool> _onWillPop() async {
    return false;
  }
}
